#include <gtest/gtest.h>

#include <network_prober/backup_test/handle_telnet_backup_test.h>

namespace tests::backup_test
{
TEST(HandleTELNETBackupTestIPv4Test, testTest)
{
    // 测试参数
    std::string interface{"192.168.0.108"};
    std::string target{"192.168.0.108"};
    std::int64_t port{23};
    std::string username{"twetec"};
    std::string password{"Twe201616"};
    std::string privilegedPassword{""};
    std::string command{"date"};

    // 探测
    network_prober::backup_test::TELNETBackupTestResultIPv4 testResult;
    std::mutex testResultMutx;
    std::condition_variable testResultsCv;

    // 测试参数
    network_prober::backup_test::TELNETBackupTestParamsIPv4 testParams{
        .interface=interface,
        .target=target,
        .port=static_cast<std::uint16_t>(port),
        .username=username,
        .password=password,
        .privilegedPassword=privilegedPassword,
        .command=command
    };

    // 创建探测指令对象
    auto backupTestInstructionPtr = std::make_shared<network_prober::backup_test::HandleTELNETBackupTestIPv4>(std::move(testParams));

    // 监听指令执行结果(延迟了对象生命周期)
    backupTestInstructionPtr->setOnTestedCallback([&testResult, &testResultMutx, &testResultsCv](network_prober::backup_test::TELNETBackupTestResultIPv4&& result) {
        // 保存指令执行结果
        std::unique_lock<std::mutex> probeResultsLock(testResultMutx);
        testResult = std::move(result);
        testResultsCv.notify_one();
    });

    // 执行指令
    backupTestInstructionPtr->test();

    // 等待完成
    std::unique_lock<std::mutex> testResultsLock(testResultMutx);
    testResultsCv.wait(testResultsLock);

    std::cout << testResult << std::endl;

    SUCCEED() << "运行成功...";
}
}